// 2025/10/02
// Author: Forever
#include <bits/stdc++.h>
using namespace std;

bitset<10001> canA[101], canB[101];
vector<pair<int, pair<int, int>>> path[101];
int n, m;

signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> m;
    for (int i = 1; i <= m; i++){
        int u, v, a, b;
        cin >> u >> v >> a >> b;
        path[u].push_back({v, {a, b}});
    }
    canA[1][0] = 1;
    canB[1][0] = 1;
    for (int i = 1; i <= n; i++){
        for (auto x : path[i]){
            int v = x.first, a = x.second.first, b = x.second.second;
            canA[v] |= (canA[i] << a);
            canB[v] |= (canB[i] << b);
        }
    }
    for (int i = 1; i <= 10000; i++){
        if (canA[n][i] && canB[n][i]){
            cout << i;
            return 0;
        }
    }
    cout << "IMPOSSIBLE";
    return 0;
}